package com.example.state;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.example.bounce.Bounce;
import com.example.bounce.R;
import com.example.glutil.WorldCoord;
import com.example.object.screen.Background;
import com.example.object.screen.SignShape;
import com.example.glsurfaceview.MyGLSurfaceView;

import org.jbox2d.common.Vec2;

/**
 * Created by Lucas on 06/06/13.
 */
public class World extends AppState {

    /**
     * The debug TAG
     */
    private final String TAG = "WorldState";

    /**
     * The constructor
     * @param context the context of the application
     * @param glView the GLSurfaceView
     */
    public World(Context context, MyGLSurfaceView glView) {
        super(context, glView);
        _bounce = (Bounce)context;
    }

    /**
     * Initialize the list of game shapes
     */
    public void initGameShape(){
        Log.i(TAG, "Initialize game's shapes");
        _listGameShape.add(new Background(_context, AppState.SIMULATION_HEIGHT/2f, R.raw.backgroundhome));
        _listGameShape.add(new SignShape(_context, 0.92f* SIMULATION_HEIGHT, 0.5f, 0.07f*SIMULATION_HEIGHT, 0.5f, "Level 1", AppState.INGAME));
        _listGameShape.add(new SignShape(_context, 0.77f* SIMULATION_HEIGHT, 0.5f, 0.07f*SIMULATION_HEIGHT, 0.5f, "Level 2", AppState.INGAME));
        _listGameShape.add(new SignShape(_context, 0.62f* SIMULATION_HEIGHT, 0.5f, 0.07f*SIMULATION_HEIGHT, 0.5f, "Level 3", AppState.INGAME));
        _listGameShape.add(new SignShape(_context, 0.47f* SIMULATION_HEIGHT, 0.5f, 0.07f*SIMULATION_HEIGHT, 0.5f, "Level 4", AppState.INGAME));
        _listGameShape.add(new SignShape(_context, 0.32f* SIMULATION_HEIGHT, 0.5f, 0.07f*SIMULATION_HEIGHT, 0.5f, "Level 5", AppState.INGAME));
        _listGameShape.add(new SignShape(_context, 0.92f* SIMULATION_HEIGHT, 1.5f, 0.07f*SIMULATION_HEIGHT, 0.5f, "Level 6", AppState.INGAME));
        _listGameShape.add(new SignShape(_context, 0.77f* SIMULATION_HEIGHT, 1.5f, 0.07f*SIMULATION_HEIGHT, 0.5f, "Level 7", AppState.INGAME));
        _listGameShape.add(new SignShape(_context, 0.62f* SIMULATION_HEIGHT, 1.5f, 0.07f*SIMULATION_HEIGHT, 0.5f, "Level 8", AppState.INGAME));
        _listGameShape.add(new SignShape(_context, 0.47f* SIMULATION_HEIGHT, 1.5f, 0.07f*SIMULATION_HEIGHT, 0.5f, "Level 9", AppState.INGAME));
        _listGameShape.add(new SignShape(_context, 0.32f* SIMULATION_HEIGHT, 1.5f, 0.07f*SIMULATION_HEIGHT, 0.5f, "Level 10", AppState.INGAME));
        _listGameShape.add(new SignShape(_context, 0.07f* SIMULATION_HEIGHT, 1.0f, 0.07f*SIMULATION_HEIGHT, 0.5f, "Back", AppState.BALL));
    }

    /**
     * handle the input touch event
     * @param e the input motion event
     * @return true if the event have been handle
     */
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        // convert into the coordinates of the object of the screen
        if (e.getAction() == MotionEvent.ACTION_DOWN){
            Vec2 touch = new Vec2(e.getX(), AppState.DEVICE_HEIGHT - e.getY());
            touch = WorldCoord.getCoordinates(_bounce, touch);
            for (int i=0; i < _listGameShape.size(); i++){
                if(_listGameShape.get(i).isTouchable()){
                    if (_listGameShape.get(i).isTouched(touch)){
                        int userData = _listGameShape.get(i).getUserData();
                        if (userData == AppState.EXIT){
                            _bounce.changeState(AppState.EXIT);
                            _currentState = EXIT;
                        }else {
                            _currentState = INGAME;
                            _bounce.setWorld(userData);
                            _bounce.changeState(AppState.INGAME);
                        }
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK )
        {
            _bounce.changeState(BALL);
            return true;
        }
        return false;
    }
}


